package org.pjsip.pjsua2.app;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.StringUtils;
import com.gwf.llsipproject.R;

import org.pjsip.constant.SipStatus;
import org.pjsip.manager.MyWindowManager;
import org.pjsip.manager.SipManager;
import org.pjsip.pjsua2.MyTimerTask;

import java.util.TimerTask;

/**
 * author : GWFan
 * time   : 1/20/21 3:13 PM
 * desc   :
 */

public class DialWindow {

    private static final String TAG = "DialWindow";

    protected MyApplication mApp;
    protected LayoutInflater mInflater;
    protected WindowManager mWindowManager;
    protected View mWindowView;

    private TextView tvStatus;

    private static DialWindow instance;

    private MyTimerTask task;
    private int i = 0;


    public static DialWindow getInstance() {
        if (instance == null) {
            synchronized (DialWindow.class) {
                if (instance == null) {
                    instance = new DialWindow();
                }
            }
        }
        return instance;
    }

    public void showDialWindowByStatus(int code) {
        this.mApp = MyApplication.getInstance();
        mInflater = LayoutInflater.from(mApp);
        if (SipStatus.SIP_STATUS_1.getKey() == code) {
            String cPhone = mApp.getPhone();
            showDialWindow(cPhone);
        } else if (SipStatus.SIP_STATUS_3.getKey() == code) {
            tvStatus.post(() -> tvStatus.setText(SipStatus.getValueByKey(code)));
        } else if (SipStatus.SIP_STATUS_5.getKey() == code) {
            try {
                i = 0;
                task = new MyTimerTask(1000, new TimerTask() {
                    @Override
                    public void run() {
                        tvStatus.post(() -> tvStatus.setText(++i));
                    }
                });
                task.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else if (SipStatus.SIP_STATUS_6.getKey() == code) {
            try {
                if (task != null) {
                    task.stop();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                SipManager.getInstance().dropCall();
            } catch (Exception e) {
                e.printStackTrace();
            }
            //通话已结束
            dismissDialWindow();
        } else {
            //呼叫失败，请重试
            try {
                if (task != null) {
                    task.stop();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            dismissDialWindow();
        }
    }

    private void showDialWindow(String cPhone) {
        dismissDialWindow();
        mWindowManager = MyWindowManager.getWindowManager();
        try {
            initDialView(cPhone);
            mWindowManager.addView(mWindowView, MyWindowManager.getWMParams(0));
        } catch (Exception e) {
            LogUtils.d("ShowDialWindow出错" + e.toString());
        }
    }

    private void initDialView(String cPhone) {
        mWindowView = mInflater.inflate(R.layout.window_dial, null);
        TextView tvPhone = mWindowView.findViewById(R.id.tv_phone);
        tvStatus = mWindowView.findViewById(R.id.tv_status);
        if (StringUtils.isEmpty(cPhone)) {
            cPhone = "未知号码";
        } else {
            cPhone = cPhone.substring(0, 3) + " " + cPhone.substring(3, 7) + " " + cPhone.substring(7);
        }
        tvPhone.setText(cPhone);
        ImageView ivHungUp = mWindowView.findViewById(R.id.iv_hung_up);
        ivHungUp.setOnClickListener(v -> {
            if (task != null) {
                task.stop();
            }
            SipManager.getInstance().dropCall();
            dismissDialWindow();
        });
    }

    private void dismissDialWindow() {
        try {
            if (mWindowManager != null && mWindowView != null) {
                mWindowManager.removeView(mWindowView);
            }
            mWindowManager = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

